//
//  UserWebService.m
//  YYZB
//
//  Created by 顾悦 on 16/3/28.
//
//

#import "UserWebService.h"

@import UIKit;

@implementation UserWebService

- (void)login:(NSString *)username
     password:(NSString *)password
completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder {
    NSString *URL = @"Customer/LoginByPsd";
    NSDictionary *params = @{
                             @"Mobile": username,
                             @"Password": password,
                             @"IMEI": [[UIDevice currentDevice].identifierForVendor UUIDString]
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            if ([items isKindOfClass:[NSDictionary class]]) {
                items = @[items];
            }
            completionHanlder(items, err);
        }
    }];
}

- (void)login:(NSString *)username
   verifyCode:(NSString *)verifyCode
 verifyCodeID:(NSString *)verifyCodeID
completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder {
    NSString *URL = @"Customer/LoginByVC";
    NSDictionary *params = @{
                             @"Mobile": username,
                             @"VerifyCode": verifyCode,
                             @"VerifyCodeId": verifyCodeID,
                             @"IMEI": [[UIDevice currentDevice].identifierForVendor UUIDString]
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            if ([items isKindOfClass:[NSDictionary class]]) {
                items = @[items];
            }
            completionHanlder(items, err);
        }
    }];
}

- (void)registe:(NSString *)username
     verifyCode:(NSString*)verifyCode
       password:(NSString *)password
   verifyCodeId:(NSString*)codeId
completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder {
    NSString *URL = @"Customer/CustomerRegister";
    NSDictionary *params = @{
                             @"UserName": username,
                             @"VerifyCode":verifyCode,
                             @"Password": password,
                             @"VerifyCodeId":codeId
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            completionHanlder(items, err);
        }
    }];
}

-(void)getVerifyCode:(NSString*)phoneNum
                flag:(NSString*)flag
   completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder{
    
    NSString *URL = @"Customer/GetVerifyCode";
    NSDictionary *params = @{
                             @"UserId": @"",
                             @"Mobile": phoneNum,
                             @"RegFlag": flag,
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            if ([items isKindOfClass:[NSDictionary class]]) {
                items = @[items];
            }
            completionHanlder(items, err);
        }
    }];
    
    
}
- (void)forgetPassword:(NSString *)username
            verifyCode:(NSString*)verifyCode
         password:(NSString *)password
          verifyCodeID:(NSString*)codeId
completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder{
    NSString *URL = @"Customer/RecoveryPassword";
    NSDictionary *params = @{
                             @"UserName": username,
                             @"VerifyCode":verifyCode,
                             @"Password": password,
                             @"VerifyCodeId":codeId
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            completionHanlder(items, err);
        }
    }];
}

- (void)recoveryPassword:(NSString *)username
              verifyCode:(NSString*)verifyCode
                password:(NSString *)password
            verifyCodeId:(NSString*)codeId
       completionHanlder:(void (^)(NSArray *items, NSError *error))completionHanlder {
    NSString *URL = @"Customer/RecoveryPassword";
    NSDictionary *params = @{
                             @"Mobile": username,
                             @"VerifyCode":verifyCode,
                             @"Password": password,
                             @"VerifyCodeId":codeId
                             };
    NSString *URLString = [self formattedURLString:params method:URL sessionNeeded:NO];
    [self sendHTTPGetRequest:URLString completionHandler:^(id result, NSError *error) {
        if (error != nil) {
            completionHanlder(nil, error);
        } else {
            NSError *err;
            NSArray *items = [self unboxedValue:result error:&err];
            completionHanlder(items, err);
        }
    }];
}

@end
